package com.example.user.controller;

import com.example.user.biz.UserBizImpl;
import com.example.user.pojo.User;

import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@Controller
public class UserController {
    @Autowired
    private UserBizImpl userBiz;

    @RequestMapping("info")
    public String info(@RequestParam("userName") String userName, HttpSession session) {
        session.setAttribute("userName", userName);
        return "redirect:getall";
    }

    @RequestMapping("/getall")
    public String all(@RequestParam(value = "pageno", defaultValue = "1") int pageno, Model model) {
        PageInfo<User> page = this.userBiz.select(pageno);
        model.addAttribute("page", page);
        return "userlist";
    }

    @RequestMapping("/modyfie")
    public String MM(long id, Model model) {
        User user = this.userBiz.selectByPrimaryKey(Math.toIntExact(id));
        model.addAttribute(user);
        return "/userupdate";
    }

    @RequestMapping("/update")
    public String updates(User user) throws IOException {


        int i = this.userBiz.updateByPrimaryKeySelective(user);
        return "redirect:getall";

    }
    @GetMapping("useradd")
    public String useradd(){

        return "useradd";

    }

    @GetMapping("/adduser")
    @ResponseBody
    public String ADDS(String userCode) {
        User user = this.userBiz.selectByUserCode(userCode);
        if (user != null) {
            return "{\"fage\":false }";
        } else {
            return "{\"fage\":true}";
        }
    }
    
    @RequestMapping("/insert")
    public String add(User user) throws IOException {
        System.out.println(user.getAddress());
        int i = this.userBiz.insertSelective(user);
        System.out.println(i);
        return "redirect:getall";
    }

    @RequestMapping("/delete")
    @ResponseBody
    public String del(String id) throws IOException {
        int i = this.userBiz.deleteByPrimaryKey(Long.valueOf(id));
        if (i > 0) {
            return "{\"fage\":true }";
        } else {
            return "{\"fage\":false }";
        }
    }

    @RequestMapping("/show")
    public String SHOWA(int id, Model model) {
        User user = this.userBiz.selectByPrimaryKey(id);
        model.addAttribute(user);
        return "/usershow";
    }

    @RequestMapping("/like")
    public String likes(User user, Model model) throws IOException {
        if (user.getUsername() == null || user.getUsername() == "" && user.getUserrole() == 0) {
            return "redirect:getall";
        } else {
            List<User> like = this.userBiz.like(user);
            model.addAttribute("page", like);
            return "userlike";
        }
    }


}
